package com.hmjk.health.views;

import com.hmjk.health.R;
import com.hmjk.health.util.BP_Evaluator;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class BpBarView extends View {
	private Context mContext;
	private int colorLBP, colorNormal, colorNormalHigh, colorHBPi, colorHBPii, colorHBPiii, colorDivision;
	private String stringLBP, stringNormal, stringNormalHigh, stringHBPi, stringHBPii, stringHBPiii;
	private ShapeDrawable bpShapeDrawable;
	private Bitmap cursorDrawable;
	private float textSize, hintSize;
	
	private int sbp = 0, dbp = 0, pulse = 0;
	private int bpValueWidthOffset = 0;
	private int offsetIndex = 0;
	private int backgroundColor = 0xFFFFFFFF;

	public BpBarView(Context context) {
		super(context);
		mContext = context;
	}
	
	public BpBarView(Context context, AttributeSet attrs) {
		// TODO Auto-generated constructor stub
		super(context, attrs);
		mContext = context;
		init(attrs);
	}
	
	public BpBarView(Context context, AttributeSet attrs, int defStyle) {
		// TODO Auto-generated constructor stub
		super(context, attrs, defStyle);
		mContext = context;
		init(attrs);
	}
	
	private void init(AttributeSet attrs){
		TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.bpbar);
		textSize = t.getDimension(R.styleable.bpbar_bpTextSize, 14);
		hintSize = t.getDimension(R.styleable.bpbar_bpHintSize, 14);
		
		bpShapeDrawable = new ShapeDrawable(new RectShape());
		
		colorLBP = mContext.getResources().getColor(R.color.BP_LBP);
		colorNormal = mContext.getResources().getColor(R.color.BP_NOR);
		colorNormalHigh = mContext.getResources().getColor(R.color.BP_NOR_HIGH);
		colorHBPi = mContext.getResources().getColor(R.color.BP_HBP_I);
		colorHBPii = mContext.getResources().getColor(R.color.BP_HBP_II);
		colorHBPiii = mContext.getResources().getColor(R.color.BP_HBP_III);
		colorDivision = mContext.getResources().getColor(R.color.WHITE);
		
		backgroundColor = colorLBP;
		
		stringLBP = mContext.getResources().getString(R.string.bp_lvl_lbp);
		stringNormal = mContext.getResources().getString(R.string.bp_lvl_normal);
		stringNormalHigh = mContext.getResources().getString(R.string.bp_lvl_normal_high);
		stringHBPi = mContext.getResources().getString(R.string.bp_lvl_hbp_mild);
		stringHBPii = mContext.getResources().getString(R.string.bp_lvl_hbp_moderate);
		stringHBPiii = mContext.getResources().getString(R.string.bp_lvl_hbp_severe);
		
		if(cursorDrawable != null){
    		cursorDrawable.recycle();
    	}
		cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_normal);
		
		setBpValue(0, 0, 0);
	}
	
	private int checkBpOffsetIndex(){
		int ret = 1;
		backgroundColor = 0xff91c869;
		
		if(cursorDrawable != null){
    		cursorDrawable.recycle();
    	}
		
		BP_Evaluator.Result r = BP_Evaluator.evaluateLevel(sbp, dbp);
		
		if((sbp == 0) || (dbp == 0)){
			backgroundColor = 0xff91c869;
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_normal);
			ret = 1;
		}else if(r.type == BP_Evaluator.TYPE_3HIGH){
			backgroundColor = 0xfff94244;
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_3high);
			ret = 5;
		}else if(r.type == BP_Evaluator.TYPE_2HIGH){
			backgroundColor = 0xfff5a93c;
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_2high);
			ret = 4;
		}else if(r.type == BP_Evaluator.TYPE_1HIGH){
			backgroundColor = 0xffe4ce3c;
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_1high);
			ret = 3;
		}else if(r.type == BP_Evaluator.TYPE_LOW){
			backgroundColor = 0xff44bde6;
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_low);
			ret = 0;
		}else if(r.type == BP_Evaluator.TYPE_NORMALHIGH){
			backgroundColor = 0xffe4e86f;
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_norhigh);
			ret = 2;
		}else{
			cursorDrawable = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bp_cursor_normal);
		}
		
		return ret;
	}
	
	public void setBpValue(int sbpvalue, int dbpvalue, int pulsevalue){       
		
        sbp = sbpvalue;
		dbp = dbpvalue;
		pulse = pulsevalue;
		offsetIndex = checkBpOffsetIndex();      
		
		invalidate();
		
	}
	
	private void drawBpBar(Canvas canvas){
        int w = getWidth();
        int drawBarMargin = w / 16;
        int wDivision = 2;
        int drawBarWidth = (w - drawBarMargin * 2 - wDivision * 5) / 6;
        int drawBarHight = drawBarWidth / 4;
        int left = drawBarMargin;
        int top = 0;
        Paint mPaint = new Paint();
        Paint textPaint = new TextPaint();
        Rect bounds = new Rect();
        w = w - drawBarMargin;
        
        int bpValueWidth = w * 3 / 10;
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        
        bpValueWidthOffset = offsetIndex * (drawBarWidth + wDivision) + drawBarWidth / 2 - bpValueWidth / 2 + drawBarMargin;
        if(bpValueWidthOffset < drawBarMargin / 2){
        	bpValueWidthOffset = drawBarMargin / 2;
        }else if((bpValueWidthOffset + bpValueWidth) > w){
        	bpValueWidthOffset = w - bpValueWidth + drawBarMargin / 2;
        }
        
        textPaint.setColor(0xFF444444);
        textPaint.setTextSize(textSize);
        String paintString = mContext.getResources().getString(R.string.SBP) + " " + String.valueOf(sbp);
        if(sbp == 0){
        	paintString = mContext.getResources().getString(R.string.SBP) + " " + mContext.getResources().getString(R.string.measure_null_value);
        }
        textPaint.getTextBounds(paintString, 0, paintString.length(), bounds);
        FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
        int fontHight = fontMetrics.bottom - fontMetrics.top;
        int bpValueHeight = fontHight * 4;
        int baseline = (int)(fontHight * 1.25);       
        
        //draw background
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2.0f);
        mPaint.setColor(backgroundColor);
        canvas.drawRect(new Rect(bpValueWidthOffset, 0, bpValueWidthOffset + bpValueWidth - 1, bpValueHeight - 1), mPaint);
        
        //draw text
        canvas.drawText(paintString, bpValueWidthOffset + bpValueWidth / 2 - bounds.width() / 2, baseline, textPaint);
        
        paintString = mContext.getResources().getString(R.string.DBP) + " " + String.valueOf(dbp);
        if(sbp == 0){
        	paintString = mContext.getResources().getString(R.string.DBP) + " " + mContext.getResources().getString(R.string.measure_null_value);
        }
        textPaint.getTextBounds(paintString, 0, paintString.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
        baseline += fontHight;
        canvas.drawText(paintString, bpValueWidthOffset + bpValueWidth / 2 - bounds.width() / 2, baseline, textPaint);
        
        paintString = mContext.getResources().getString(R.string.pulse) + " " + String.valueOf(pulse);
        if(sbp == 0){
        	paintString = mContext.getResources().getString(R.string.pulse) + " " + mContext.getResources().getString(R.string.measure_null_value);
        }
        textPaint.getTextBounds(paintString, 0, paintString.length(), bounds);
        baseline += fontHight;
        canvas.drawText(paintString, bpValueWidthOffset + bpValueWidth / 2 - bounds.width() / 2, baseline, textPaint);
        
        top += bpValueHeight;
        
        int cursorh = cursorDrawable.getHeight();
        int cursorw = cursorDrawable.getWidth();
        textPaint.setTextSize(hintSize);
        left = drawBarMargin;
        
        canvas.drawBitmap(cursorDrawable, drawBarMargin + offsetIndex * (drawBarWidth + wDivision) + (drawBarWidth - cursorw) / 2, top, mPaint);
        
        top += cursorh;
        top += 2 * wDivision;
        
        //draw LBP
        bpShapeDrawable.getPaint().setColor(colorLBP);
        bpShapeDrawable.setBounds(left, top, left + drawBarWidth, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        textPaint.getTextBounds(stringLBP, 0, stringLBP.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
    	baseline = top + drawBarHight + fontMetrics.bottom - fontMetrics.top;
    	canvas.drawText(stringLBP, left + (drawBarWidth - bounds.width()) / 2, baseline, textPaint);
    	
    	left += drawBarWidth;
        
        //draw division
    	/*
        bpShapeDrawable.getPaint().setColor(colorDivision);
        bpShapeDrawable.setBounds(left, top, left + wDivision, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        */
        left += wDivision;
        
        //draw NOR
        bpShapeDrawable.getPaint().setColor(colorNormal);
        bpShapeDrawable.setBounds(left, top, left + drawBarWidth, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        textPaint.getTextBounds(stringNormal, 0, stringNormal.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
    	baseline = top + drawBarHight + fontMetrics.bottom - fontMetrics.top;
    	canvas.drawText(stringNormal, left + (drawBarWidth - bounds.width()) / 2, baseline, textPaint);
        
        left += drawBarWidth;
        
        //draw division
        /*
        bpShapeDrawable.getPaint().setColor(colorDivision);
        bpShapeDrawable.setBounds(left, top, left + wDivision, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        */
        left += wDivision;
        
        //draw NOR HIGH
        bpShapeDrawable.getPaint().setColor(colorNormalHigh);
        bpShapeDrawable.setBounds(left, top, left + drawBarWidth, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        textPaint.getTextBounds(stringNormalHigh, 0, stringNormalHigh.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
    	baseline = top + drawBarHight + fontMetrics.bottom - fontMetrics.top;
    	canvas.drawText(stringNormalHigh, left + (drawBarWidth - bounds.width()) / 2, baseline, textPaint);
    	
        left += drawBarWidth;
        
        //draw division
        /*
        bpShapeDrawable.getPaint().setColor(colorDivision);
        bpShapeDrawable.setBounds(left, top, left + wDivision, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        */
        left += wDivision;
        
        //draw HBP I
        bpShapeDrawable.getPaint().setColor(colorHBPi);
        bpShapeDrawable.setBounds(left, top, left + drawBarWidth, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        textPaint.getTextBounds(stringHBPi, 0, stringHBPi.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
    	baseline = top + drawBarHight + fontMetrics.bottom - fontMetrics.top;
    	canvas.drawText(stringHBPi, left + (drawBarWidth - bounds.width()) / 2, baseline, textPaint);
    	
        left += drawBarWidth;
        
        //draw division
        /*
        bpShapeDrawable.getPaint().setColor(colorDivision);
        bpShapeDrawable.setBounds(left, top, left + wDivision, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        */
        left += wDivision;
        
        //draw HBP II
        bpShapeDrawable.getPaint().setColor(colorHBPii);
        bpShapeDrawable.setBounds(left, top, left + drawBarWidth, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        textPaint.getTextBounds(stringHBPii, 0, stringHBPii.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
    	baseline = top + drawBarHight + fontMetrics.bottom - fontMetrics.top;
    	canvas.drawText(stringHBPii, left + (drawBarWidth - bounds.width()) / 2, baseline, textPaint);
    	
        left += drawBarWidth;
        
        //draw division
        /*
        bpShapeDrawable.getPaint().setColor(colorDivision);
        bpShapeDrawable.setBounds(left, top, left + wDivision, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        */
        left += wDivision;
        
        //draw HBP III
        bpShapeDrawable.getPaint().setColor(colorHBPiii);
        bpShapeDrawable.setBounds(left, top, left + drawBarWidth, top + drawBarHight);
        bpShapeDrawable.draw(canvas);
        textPaint.getTextBounds(stringHBPiii, 0, stringHBPiii.length(), bounds);
        fontMetrics = textPaint.getFontMetricsInt();
    	baseline = top + drawBarHight + fontMetrics.bottom - fontMetrics.top;
    	canvas.drawText(stringHBPiii, left + (drawBarWidth - bounds.width()) / 2, baseline, textPaint);
        
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		drawBpBar(canvas);
	}

}
